# -*- coding: utf-8 -*-
"""
btc wallet bot Project

This project was developed by Seye (cescwalletbot@protonmail.com)

Dependencies:
pip pytelegrambotapi
pip pypng
pip pyqrcode
pip certifi
pip urllib3

"""

import json
import cauxiliary


def send_reply(txid, username, sent_amount, fee_amount):
    language = json.load(open('./log/userpref/' + str(username) + '.json'))['lang']
    if txid == 0:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_1']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_1']
        return reply
    if txid == 10:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_2']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_2']
        return reply
    if txid == 20:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_3']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_3']
        return reply
    if txid == 30:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_4']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_4']
        return reply
    if txid == 40:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_5']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_5']
        return reply
    if txid == 50:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_6']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_6']
        return reply
    if txid == 60:
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_7']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['send_reply_7']
        return reply
    if txid is not 0 or 10 or 20 or 30 or 40 or 50 or 60:
        txid = str(txid)
        txid = txid[2:len(txid) - 3]
        total = float(sent_amount) + float(fee_amount)
        total = total * -1
        if sent_amount < 0:
            sent_amount = sent_amount * -1
        if fee_amount < 0:
            fee_amount = fee_amount * -1
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['send_reply_8'] + txid
            reply = reply + json.load(open('./translations/en.json', 'r'))['send_reply_9'] + str(total)
            reply = reply + json.load(open('./translations/en.json', 'r'))['send_reply_10'] + str(sent_amount) + json.load(open('./translations/en.json', 'r'))['send_reply_11'] + str(fee_amount)
        if language == 'pt':
           json.load(open('./translations/pt.json', 'r'))['send_reply_8'] + txid
           reply = reply + json.load(open('./translations/pt.json', 'r'))['send_reply_9'] + str(total)
           reply = reply + json.load(open('./translations/pt.json', 'r'))['send_reply_10'] + str(sent_amount) + json.load(open('./translations/pt.json', 'r'))['send_reply_11'] + str(fee_amount)
        return reply


def transactions_reply(transactions, username):
    """organize the wallet raw output to give a reply to users"""
    language = json.load(open('./log/userpref/' + str(username) + '.json', 'r'))['lang']
    if transactions == '10':
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['transactions_reply_2']
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['transactions_reply_2']
        return reply
    if transactions != '10':
        check = str(transactions)
        amount_count = 0
        address_count = 0
        amount_compile = ['', '', '', '', '']
        address_compile = ['', '', '', '', '']
        for x in range(0, len(check), 1):
            address_check = check[x:x + 7]
            amount_check = check[x:x + 6]
            if address_check == 'address':
                address_compile[address_count] = str(check[x + 12:x + 12 + 34])
                address_count = address_count + 1
            if amount_check == 'amount':
                for z in range(len(check), 0, -1):
                    s = check[x + 10:x + 10 + z]
                    if cauxiliary.is_float(s) is True:
                        amount_compile[amount_count] = str(s)
                        amount_count = amount_count + 1
                        break
        if language == 'en':
            reply = json.load(open('./translations/en.json', 'r'))['transactions_reply_3']
            reply = reply + '\n' + json.load(open('./translations/en.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[0] + '   ' + json.load(open('./translations/en.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[0]
            if amount_compile[1] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/en.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[1] + '   ' + json.load(open('./translations/en.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[1]
            if amount_compile[2] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/en.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[2] + '   ' + json.load(open('./translations/en.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[2]
            if amount_compile[3] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/en.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[3] + '   ' + json.load(open('./translations/en.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[3]
            if amount_compile[4] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/en.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[4] + '   ' + json.load(open('./translations/en.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[4]
                return reply
        if language == 'pt':
            reply = json.load(open('./translations/pt.json', 'r'))['transactions_reply_3']
            reply = reply + '\n' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[0] + '   ' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[0]
            if amount_compile[1] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[1] + '   ' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[1]
            if amount_compile[2] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[2] + '   ' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[2]
            if amount_compile[3] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[3] + '   ' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[3]
            if amount_compile[4] == '':
                return reply
            else:
                reply = reply + '\n' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_4'] + '   ' + amount_compile[4] + '   ' + json.load(open('./translations/pt.json', 'r'))['transactions_reply_5'] + '   ' + address_compile[4]
                return reply